﻿
@{
    ViewBag.Title = "军械新增";

}
<link rel="stylesheet" href="../../Customiz/layui/css/layui.css" media="all">

<form class="layui-form" action="" lay-filter="example">
    <div class="layui-form" lay-filter="layuiadmin-form-admin" id="layuiadmin-form-admin" style="padding: 20px 30px 0 0;">
        <div class="layui-form-item">
            <label class="layui-form-label">军械名称</label>
            <div class="layui-input-inline">
                <input type="text" name="Army_Name" id="Army_Name" lay-verify="required" placeholder="请输入军械名称" autocomplete="off" class="layui-input">
            </div>
        </div>   
        <div class="layui-form-item">
            <label class="layui-form-label">维护周期(月)</label>
            <div class="layui-input-inline">
                <input type="text" name="Army_RepairCycle" id="Army_RepairCycle" lay-verify="required|num" placeholder="请输入维护周期" autocomplete="off" class="layui-input">
            </div>
        </div>     
       <div>
            <button class="layui-btn layui-btn-primary layui-btn-sm"  id="Ord_Quxiao" style="float:right">取消</button>
           <button class="layui-btn layui-btn-normal layui-btn-sm" lay-submit lay-filter="Ord-Army-Add-search" style="float:right;margin-right:20px">确定</button>
        </div>
    </div>
</form>
<script src="../../Customiz/scripts/jquery-3.4.1.min.js"></script>
<script src="../../Customiz/layui/layui.js"></script>  
<script>
    
    $(function(){
        if(@ViewBag.Type==2)
        {
            $("#Army_Name").val('@ViewBag.ArmyName');
            $("#Army_RepairCycle").val('@ViewBag.ArmyRepairCycle');
        }
    });

    //根据名称获取是否军械名称重复
    function selectcount(value) {
        var count = 0;
        $.ajax({
            url: '/Army/GetArmyCount',
            dataType: 'json',
            type: 'Post',
            async: false,
            data: { Army_Name: value },
            success: function (data) {
                count= data;
            }
        })
        return count;
    }


    layui.use(['table'], function () {
        var table = layui.table;
        var form = layui.form;
        var layer = layui.layer;

       
        
        $("#Ord_Quxiao").click(function () {
            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引

            parent.layer.close(index); //再执行关闭
        });


        //表单验证
        form.verify({
            num: function (value) {
                if (!/^[0-9]+$/.test(value)) {
                    return '请输入数字！';
                } else {
                    if (Number(value) > 12) {
                        return'维护周期不能超过12个月！';
                    }
                }

            }

        });

        //监听提交
        form.on('submit(Ord-Army-Add-search)', function (data) {
            var field = data.field;
            //提交 Ajax 成功后，静态更新表格中的数据
            if(@ViewBag.Type==1)
            {
                if (selectcount(field.Army_Name) > 0)
                {
                    layer.msg('军械名称存在重复！', { icon: 7 });;
                }
                else
                {
                    $.ajax({
                        cache: false,
                        type: "Post",
                        async: false,
                        url: '/Army/AddArmy', //模拟接口
                        data: {
                            Army_Name: field.Army_Name,
                            Army_RepairCycle: field.Army_RepairCycle
                        },
                        dataType: 'json',
                        success: function (data) {
                            if (data == true) {
                                parent.layer.msg("新增成功！", { icon: 1 });

                                window.parent.$(".layui-laypage-btn").click();//刷新父窗体列表
                                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                parent.layer.close(index); //再执行关闭


                                

                            } else {
                                parent.layer.msg("新增失败！", { icon: 2 });
                            }
                        }
                    })
                }
            }else
            {
                $.ajax({
                    cache: false,
                    type: "Post",
                    async: false,
                    url: '/Army/UpdateArmy', //模拟接口
                    data: {
                        Army_ID:"@ViewBag.ArmyID",
                        Army_Name: field.Army_Name,
                        Army_RepairCycle: field.Army_RepairCycle
                    },
                    dataType: 'json',
                    success: function (data) {
                        if (data == true) {
                            parent.layer.msg("修改成功！", { icon: 1 });

                            window.parent.$(".layui-laypage-btn").click();//刷新父窗体列表

                            var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                            parent.layer.close(index); //再执行关闭
                            


                        } else {
                            parent.layer.msg("修改失败！", { icon: 2 });
                        }
                    }
                })
            }

         
            
        });


    });
    </script>